<?php
declare(strict_types = 1);
namespace app\common\controller\api;

use app\common\controller\ApiBase;
use app\common\wormview\AddEditList;
use app\facade\hook\Common;
use worm\NodeFormat;

abstract class Part extends ApiBase {
    use AddEditList;
    protected $basename;
    protected $ArtMdodelBase;
    protected $ArtMdodelList;
    protected $validate;
    protected $list_page = false;
    protected function initialize(){
        parent::initialize();
        //  定义模型
        preg_match_all('/([_a-z]+)/',toUnderScore(get_called_class()),$array);
        $this->basename = $array['0']['3'];
        $mdodel = "app\\common\\model\\".$this->basename."\\".ucfirst($array['0']['4']);
        $ArtMdodelBase = "app\\common\\model\\".$this->basename."\\Artmodel";
        $this->validate = "app\\common\\validate\\ArtPart";
        $this->model = new $mdodel;
        $this->ArtMdodelBase = new $ArtMdodelBase;
        $this->ArtMdodelList = $this->ArtMdodelBase->getList(['status' => '1']);
    }
    protected function getMap(){
        $mid = empty($this->getdata['mid']) ? array_column($this->ArtMdodelList,'id') : $this->getdata['mid'];
        $map = [
            'mid' => empty($mid) ? 'no' : $mid,
            'del_time' => empty($this->getdata['del_time']) ? '' : $this->getdata['del_time'],
        ];
        return array_merge(parent::getMap(),$map); // TODO: Change the autogenerated stub
    }
    public function index()
    {
        $_id = empty($this->getdata['id']) ? '' : $this->getdata['id'];
        unset($this->getdata['id']);
        $map = array_merge($this->getMap(),$this->getdata);
        $getlist = $this->model->getList($map);
        if(empty($getlist)){
            $this->result('','0','栏目不存在');
        }
        $getlist = NodeFormat::toList($getlist);
        if(!empty($_id)){
            $_getlist = Common::del_file($getlist,'id',$_id);
            $getlist = NodeFormat::getChilds($getlist,$_id);
            $getlist = array_merge($_getlist,$getlist);
        }
        return $this->viewApiList($getlist);
    }
    public function read()
    {
        if(empty($this->getdata['id'])){
            $this->result('','0','非法访问');
        }
        $getlist = $this->model->getList(['id' => $this->getdata['id']]);
        if(empty($getlist['0']) || $getlist['0']['status'] != '1'){
            $this->result('','0','栏目不存在或已禁用');
        }
        $getlist = $getlist['0'];
        if($getlist['class'] == '1'){
            $getlist['content'] = $getlist['description'];
            $getlist['description'] = str_replace(PHP_EOL, '', $getlist['description']);
            $getlist['description'] = preg_replace('/<([^<]*)>/is',"",$getlist['description']);
            $getlist['description'] = preg_replace('/ |　|&nbsp;/is',"",$getlist['description']);	//把多余的空格去除掉
            $getlist['description'] = preg_replace('/\s/is',"",$getlist['description']);
            $getlist['description'] = get_word($getlist['description'],300,false);	//把多余的空格去除掉
        }
        return $this->viewApiRead($getlist);
    }
}